package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.GuangGaoBeen;
import com.example.scity.beens.MovieBeen;
import com.example.scity.beens.NewsBeen;
import com.example.scity.beens.NewsTypeBeen;
import com.example.scity.beens.ShjfBannerBeen;
import com.example.scity.beens.ShjfBeen;
import com.example.scity.beens.ShjfTypeBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShjfActivity extends AppCompatActivity {
    View view;
    Banner shjfbanner;
    ListView listView;
    ShjfTypeBeen shjfTypeBeen;
    ShjfBeen shjfBeen;
    ShjfBeen shjfBeen2;
    RadioGroup radioGroup;
    LinearLayout shjfyjfklinear,shjftqyblinear,shjfjflinear,shjfhfczlinear;
    RadioButton shjf_xwradioButton,shjf_ssradioButton,shjf_jyradioButton,shjf_qcradioButton,shjf_fcradioButton;
    List<String> imglist=new ArrayList<>();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    ShjfListView();
                    break;
                case 1:
                    handlerbanner();
                    break;
                case 2:
                    //DisplayShjfType();
                    break;
                case 3:
                    //DisplayNewsList();
                    break;
            }
        }
    };

    private void ShjfListView() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return shjfBeen2.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return shjfBeen2.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(ShjfActivity.this).inflate(R.layout.item_shjf, null);
                TextView title;
                title = view.findViewById(R.id.item_shjf_title);
                title.setText(shjfBeen2.rows.get(i).newsTitle);
                TextView user;
                user = view.findViewById(R.id.item_shjf_userName);
                user.setText("用户名:"+shjfBeen2.rows.get(i).userName);
                TextView content;
                content = view.findViewById(R.id.item_shjf_content);
                content.setText(Html.fromHtml(shjfBeen2.rows.get(i).content));
                TextView pltime;
                pltime = view.findViewById(R.id.item_shjf_commentDate);
                pltime.setText("评论时间:"+shjfBeen2.rows.get(i).commentDate);
                TextView likeNum;
                likeNum = view.findViewById(R.id.item_shjf_likeNum);
                likeNum.setText("点赞数："+shjfBeen2.rows.get(i).likeNum);
                return view;
            }
        });
    }

    private void handlerbanner() {
        shjfbanner.setImages(imglist);
        shjfbanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context).load(o).into(imageView);
            }
        });
        shjfbanner.setDelayTime(2000);
        shjfbanner.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shjf);
        shjfbanner=findViewById(R.id.shjf_banner);
        listView = findViewById(R.id.shjf_listView);
        loadshjfbanner();
        LoadShjflist();//获取新闻列表
        loadshjfyjfklinear();
        loadshjftqyblinear();
        loadshjfjflinear();
        loadshjfhfczlinear();
       // LoadShjfTypes();//加载新闻类别

        //InitRadioGroup();//初始化radiogroup

    }
    private void loadshjfhfczlinear() {
        shjfhfczlinear=findViewById(R.id.shjfhfczlinear);
        shjfhfczlinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShjfActivity.this,ShjfHfczActivity.class));
            }
        });
    }
    private void loadshjfyjfklinear() {
        shjfyjfklinear=findViewById(R.id.shjfyjfklinear);
        shjfyjfklinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShjfActivity.this,ShjfYjfkActivity.class));
            }
        });
    }
    private void loadshjfjflinear() {
        shjfjflinear=findViewById(R.id.shjfjflinear);
        shjfjflinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShjfActivity.this,ShjfJfActivity.class));
            }
        });
    }
    private void loadshjftqyblinear() {
        shjftqyblinear=findViewById(R.id.shjftqyblinear);
        shjftqyblinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShjfActivity.this,ShjgTqybActivity.class));
            }
        });
    }

    private void LoadShjflist() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.ShjfList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                shjfBeen2 = new Gson().fromJson(response.body().string(), ShjfBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }



    //分类
    private void DisplayShjfType() {
        for(int i = 0 ;i<shjfTypeBeen.data.size();i++){
            switch (i){
                case 0:
                    shjf_xwradioButton=findViewById(R.id.shjf_xwradioButton);
                    shjf_xwradioButton.setText(shjfTypeBeen.data.get(i).name);
                    break;
                case 1:
                    shjf_ssradioButton=findViewById(R.id.shjf_ssradioButton);
                    shjf_ssradioButton.setText(shjfTypeBeen.data.get(i).name);
                    break;
                case 2:
                    shjf_jyradioButton=findViewById(R.id.shjf_jyradioButton);
                    shjf_jyradioButton.setText(shjfTypeBeen.data.get(i).name);
                    break;
                case 3:
                    shjf_fcradioButton=findViewById(R.id.shjf_fcradioButton);
                    shjf_fcradioButton.setText(shjfTypeBeen.data.get(i).name);
                    break;
                case 4:
                    shjf_qcradioButton=findViewById(R.id.shjf_qcradioButton);
                    shjf_qcradioButton.setText(shjfTypeBeen.data.get(i).name);
                    break;
                default:
                    return;
            }
        }
        radioGroup.check(R.id.shjf_xwradioButton);
    }


    private void InitShjfRadioGroup() {
        radioGroup = view.findViewById(R.id.shjfradiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(shjfTypeBeen==null)return;
                switch (checkedId){
                    case R.id.shjf_xwradioButton:
                        if(shjfTypeBeen.data.size()>=1){
                            LoadShjf(shjfTypeBeen.data.get(0).id);
                        }
                        break;
                    case R.id.shjf_ssradioButton:
                        if(shjfTypeBeen.data.size()>=2){
                            LoadShjf(shjfTypeBeen.data.get(1).id);
                        }
                        break;
                    case R.id.shjf_jyradioButton:
                        if(shjfTypeBeen.data.size()>=3){
                            LoadShjf(shjfTypeBeen.data.get(2).id);
                        }
                        break;
                    case R.id.shjf_fcradioButton:
                        if(shjfTypeBeen.data.size()>=4){
                            LoadShjf(shjfTypeBeen.data.get(3).id);
                        }
                        break;
                    case R.id.shjf_qcradioButton:
                        if(shjfTypeBeen.data.size()>=5){
                            LoadShjf(shjfTypeBeen.data.get(4).id);
                        }
                        break;
                }
            }
        });
        //radioGroup.check(R.id.radioButton);
    }

    private void LoadShjf(int type) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.ShzxList + "?type="+type).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                shjfBeen = new Gson().fromJson(response.body().string(), ShjfBeen.class);
                handler.sendEmptyMessage(3);//加载成功
            }
        });
    }

    private void LoadShjfTypes() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.ShjfTypeList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                shjfTypeBeen = new Gson().fromJson(response.body().string(),ShjfTypeBeen.class);
                handler.sendEmptyMessage(2);//加载成功
            }
        });
    }


    private void loadshjfbanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.ShjfBannerList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                ShjfBannerBeen json = new Gson().fromJson(response.body().string(), ShjfBannerBeen.class);
                for (int i = 0;i<json.rows.size();i++){
                    imglist.add(Configs.Host+json.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}